package de.wbh.gwt.form.layouter.shared.place;

import java.util.Iterator;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceTokenizer;

import de.wbh.gwt.form.layouter.shared.Step;

public class CreateConfigurationPlace extends Place {

	public static class Tokenizer implements PlaceTokenizer<CreateConfigurationPlace> {

		@Override
		public CreateConfigurationPlace getPlace(String token) {
			Iterator<String> parts = Splitter.on("(").split(token).iterator();
			String stepString = parts.next();
			CreateConfigurationPlace place = new CreateConfigurationPlace(Step.valueOf(stepString));
			if (parts.hasNext()) {
				String argsString = parts.next();
				argsString = argsString.substring(0, argsString.length() - 1);
				for (String arg : Splitter.on(",").split(argsString)) {
					Iterator<String> argParts = Splitter.on("=").split(arg).iterator();
					String key = argParts.next();
					String value = argParts.next();
					if ("efd".equals(key)) {
						place.setEfdName(value);
					} else if ("pdf".equals(key)) {
						place.setPdfName(value);
					}
				}

			}
			return place;

			//
			//
			// Iterable<String> splitt = Splitter.on(":").split(token);
			// int i = 0;
			// String step = null;
			// String pdfName = null;
			// for (String part : splitt) {
			// i++;
			// if (i == 1) {
			// step = part;
			// } else if (i == 2) {
			// pdfName = part;
			// }
			// }
			// return new CreateConfigurationPlace(Step.valueOf(step), pdfName);

		}

		@Override
		public String getToken(CreateConfigurationPlace place) {
			String token = place.step.toString();
			String pdfArg = Strings.isNullOrEmpty(place.pdfName) ? null : "pdf=" + place.pdfName;
			String efdArg = Strings.isNullOrEmpty(place.efdName) ? null : "efd=" + place.efdName;
			String args = Joiner.on(",").skipNulls().join(pdfArg, efdArg);
			if (!args.isEmpty()) {
				token = token + "(" + args + ")";
			}
			return token;
		}

	}

	public static CreateConfigurationPlace create() {
		return new CreateConfigurationPlace(Step.UPLOAD_PDF);
	}

	private String pdfName;

	private String efdName;

	private final Step step;

	private CreateConfigurationPlace(Step step) {
		this(step, null);
	}

	private CreateConfigurationPlace(Step step, String pdfName) {
		this.step = step;
		this.pdfName = pdfName;
	}

	public String getEfdName() {
		return this.efdName;
	}

	public String getPdfName() {
		return this.pdfName;
	}

	public Step getStep() {
		return this.step;
	}

	public CreateConfigurationPlace next() {
		CreateConfigurationPlace next;
		if (this.step == Step.UPLOAD_PDF) {
			next = new CreateConfigurationPlace(Step.UPLOAD_FIELDS);
		} else {
			next = new CreateConfigurationPlace(Step.SAVE);
		}

		next.setEfdName(this.efdName);
		next.setPdfName(this.pdfName);

		return next;
	}

	public void setEfdName(String efdName) {
		this.efdName = efdName;
	}

	public void setPdfName(String pdfName) {
		this.pdfName = pdfName;
	}

}
